// pages/label/login/login.js
const {
    baseUrl,
    isHaveToken,
    isHaveLogin
} = require("../../../utils/util")
import Message from 'tdesign-miniprogram/message/index';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        nickName: "",
        sign: "",
        image: 'https://cdn-we-retail.ym.tencent.com/retail-ui/components-exp/avatar/avatar-v2/1.png',
        isLogin: false,
        // isChage:false,
        dialogKey: '',
        input: "修改个性标语",
        showWithNickName: false,
        showWithSing: false,
        showTextAndTitleWithInput: false,
        visible: false,
        showIndex: false,
        closeBtn: false,
        deleteBtn: false,
        images: [],
    },
    clickHandle() {
        this.setData({
            images: [this.data.userInfo.avatar],
            showIndex: true,
            visible: true,
        });
    },
    onChange(e) {
        const {
            detail: { index },
        } = e;
        Toast({
            context: this,
            selector: '#t-toast',
            message: `翻到第${index + 1}个`,
        });
    },

    onDelete(e) {
        const {
            detail: { index },
        } = e;
        Toast({
            context: this,
            selector: '#t-toast',
            message: `删除第${index + 1}个`,
        });
    },

    onClose(e) {
        const {
            detail: { trigger },
        } = e;
        if (trigger === 'overlay') {
            Toast({
                context: this,
                selector: '#t-toast',
                message: '点击overlay关闭',
            });
        } else if (trigger === 'button') {
            Toast({
                context: this,
                selector: '#t-toast',
                message: `点击button关闭`,
            });
        }
        this.setData({
            visible: false,
        });
    },
    showDialog(e) {
        console.log("showDialog", e)
        const {
            key
        } = e.currentTarget.dataset;
        this.setData({
            [key]: true,
            dialogKey: key
        });
    },

    closeDialog(e) {
        console.log("closeDialog", e)
        const {
            dialogKey
        } = this.data;
        this.setData({
            [dialogKey]: false
        });
    },

    //修改昵称
    NickNameChange(e) {
        console.log("userInfo", this.data.userInfo)
        console.log(e)
        var that = this
        const {
            dialogKey
        } = this.data;
        this.setData({
            [dialogKey]: false
        });
        console.log("修改昵称", e)
        console.log("要网络修改昵称啦", this.data.nickName)
        // if(e.)
        if (this.data.nickName != "") {
            wx.request({
                url: `${baseUrl}/ucenter/member/changeInfo`,
                method: "POST",
                data: {
                    id: this.data.userInfo.id,
                    // id: "1195187659054329857",
                    nickname: this.data.nickName
                },
                success(res) {
                    console.log("NickNameChange", res)
                    that.setData({
                        // isChage:true,
                        nickName: ""
                    })
                    that.getUserInfo()
                }
            })
        } else {
            wx.showToast({
                title: '内容为空',
                icon: 'error',
                duration: 1000
            })
        }
    },
    //获得input修改昵称
    GetNickNameChange(e) {
        console.log("获得input修改昵称", e.detail)
        this.setData({
            nickName: e.detail.value
        })
    },
    //获得input修改个性标语
    GetSingChange(e) {
        var that = this
        console.log("获得input修改个性标语", e)
        this.setData({
            sign: e.detail.value
        })
        console.log("要网络修改个性标语啦", this.data.sign)
    },

    //修改个性标语
    SingChange(e) {
        console.log(e)
        var that = this
        const {
            dialogKey
        } = this.data;
        this.setData({
            [dialogKey]: false
        });
        console.log("修改个性标语", e)
        if (this.data.sign != "") {
            wx.request({
                url: `${baseUrl}/ucenter/member/changeInfo`,
                method: "POST",
                data: {
                    id: this.data.userInfo.id,
                    // id: "1195187659054329857",
                    sign: this.data.sign
                },
                success(res) {
                    console.log("GetSingChange", res)
                    that.setData({
                        // isChage:true,
                        sign: ""
                    })
                    that.getUserInfo()
                }
            })
        } else {
            wx.showToast({
                title: '内容为空',
                icon: 'error',
                duration: 1000
            })
        }
    },

    changeAvatar(events) {
        console.log(events.currentTarget.dataset)
        var myInfo = JSON.stringify(events.currentTarget.dataset)
        wx.navigateTo({
            url: '/pages/avatar/avatarChange?avatar=' + myInfo,
            events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },

    showIconMessage(e) {
        console.log(e)
        Message.info({
            context: this,
            offset: ['20rpx', '32rpx'],
            duration: 3000,
            icon: 'error-circle',
            content: '未登录自动跳转到登陆页',
        });
    },
    getUserInfo() {
        var that = this
        wx.request({
            header: {
                token: wx.getStorageSync('token')
            },
            url: `${baseUrl}/ucenter/member/getMemberInfo`,
            success(res) {
                console.log("userInfo", res)
                that.setData({
                    userInfo: res.data.data.userInfo,
                    isLogin: true
                }),
                    wx.setStorageSync('userInfo', res.data.data.userInfo)
            }
        })
    },
    quit() {
        wx.clearStorage({
            success: (res) => {
                wx.reLaunch({
                    url: '../register/register',
                })
            },
        })
    },
    login() {
        wx.navigateTo({
            url: '../register/register',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log("isHaveToken", isHaveToken())
        console.log("isHaveLogin", isHaveLogin())
        if (isHaveToken()) {
            console.log("已登录")
            this.getUserInfo()
        } else {
            wx.showToast({
                title: '未登录',
                duration: 1000,
                icon: "loading",
                success: (res) => {
                    this.showIconMessage()
                    wx.navigateTo({
                        url: '../register/register',
                    })
                    console.log("res", res)
                },
                fail: (res) => { },
                complete: (res) => { },
            })

        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        if (isHaveToken() || isHaveLogin()) {
            console.log("已登录")
            this.getUserInfo()
            this.setData({
                isLogin: true
            })
        } else {
            wx.showToast({
                title: '未登录',
                duration: 1000,
                icon: "loading",
                success: (res) => {
                    this.showIconMessage()
                    wx.navigateTo({
                        url: '../register/register',
                    })
                    console.log("res", res)
                },
                fail: (res) => { },
                complete: (res) => { },
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})